package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.CommonActivityMgr;
import com.yanqu.road.server.manger.activity.bowart.BowArtMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class CrossCommonActivityRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        List<ActivityInfo> activityInfoList = CommonActivityMgr.getAllOpenCommonActivityInfoList();
        if (activityInfoList != null) {
            for (ActivityInfo activityInfo : activityInfoList) {
                if (ActivityMgr.activityInTime(activityInfo) && activityInfo.getType() == activityType) {
                    List<Integer> commonActivityItemList = activityInfo.getCommonActivityItemList();
                    for (Integer itemId : commonActivityItemList) {
                        List<String> paramList = new ArrayList<>();
                        long recycleTime = activityInfo.getEndTime() * 1000L + 600 * 1000;
                        paramList.add(activityInfo.getActivityName());
                        ActivityItemRecycleMgr.addRecycleItemData(itemId, activityType, 1, recycleTime, paramList);
                    }
                }
            }
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.CrossCommonActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        removeCommonActivityBagItem(userId, data);
    }

    public void removeCommonActivityBagItem(long userId, ActivityItemRecycleData data) {
        Property reward = new Property();
        long count;
        long modifyTime;
        int goodsId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, goodsId);
        if (null == userBagItem || userBagItem.getNum() <= 0) {
            return;
        }
        count = userBagItem.getNum();
        modifyTime = userBagItem.getModifyTime();
        for (int i = 0; i < count; i++) {
            OpenGoodsBagResult result = GoodsBagMgr.getRandomGoods(goodsId);
            reward.addProperty(result.getReward());
        }
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        String language = UserMgr.getUserInfo(userId).getLanguage();
        if (!StringUtils.isNullOrEmpty(rewardStr) && removeItem(userBagItem)) {//有将道具转换
            String goodsName = "";
            GoodsInfo goodsInfo = GoodsMgr.getGoodsById(goodsId);
            if (null != goodsInfo) {
                goodsName = ServerLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
            } else {
                log.error("remove common activity goods to score,goods no find,goodsId:{},remove count:{}", goodsId, count);
            }
            List<String> paramList = data.getParamList();
            String activityName = paramList.get(0);
            activityName = ServerLanguageMgr.getContent(activityName, language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_ACTIVITY_GOODS_AUTO_CONSUME_TITLE, language),
                    activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_ACTIVITY_GOODS_AUTO_CONSUME_CONTENT, language),
                    activityName, goodsName, count);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove common activity goods to score reward is empty,consume goodsId:{},count:{}", goodsId, count);
            }
        }
        LogMgr.addLogCommonActivityGoodsAutoConsume(userId, goodsId, count, modifyTime, rewardStr);
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
